// pages/evaluatePaging/index.js
const windowWidth = wx.getSystemInfoSync().windowWidth;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activeTab: 0, //选中tab下标
    tabTitles: ["全部", "待评价", "已评价"],
    bodyScrollLeft: 0,
    endTab: 0,
    toView: '',
    currentLeft: 0,
    isTouching: true,
    currentTab: 0,
    haveEvaluatedDrugData: [],
    waitEvaluateDrugData: [],
    moveData: null,
    allDrugData: [{
      name: '二甲双胍',
      update: '2018-09-12 12:23',
      state: 0,
      id: 'dhf197863612usd'

    },
    {
      name: '二甲双胍',
      update: '2018-09-12 12:23',
      state: 1,
      id: 'dhf197863612usd'

    }, {
      name: '二甲双胍',
      update: '2018-09-12 12:23',
      state: 0,
      id: 'dhf197863612usd'

    }]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setListData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 点击tab
  */
  selectTab: function (e) {
    console.log(e);
    this.moveClick(e.currentTarget.dataset.index)
    //设置选中样式
    let toIndex = e.currentTarget.dataset.index;
    let fromIndex = this.data.activeTab;
    if (toIndex === fromIndex) {
      return;
    }
    let offSetLeft = e.currentTarget.offsetLeft;
    this.scrollTopBar(offSetLeft, toIndex);
    this.updatePage(fromIndex, toIndex);
  },
  /**
   * 更新展示页面及选中样式
   */
  updatePage: function (fromIndex, toIndex) {
    // body
    let bodyScrollLeft = toIndex * windowWidth;
    this.setData({
      activeTab: toIndex,
      bodyScrollLeft: bodyScrollLeft,
    });
  },
  /**
   * 自适应tabBar选中位置
   */
  scrollTopBar: function (offSetLeft, index) {
    let that = this;
    var nodeId = "#item-" + index;

    wx.createSelectorQuery().select(nodeId).boundingClientRect(function (rect) {
      console.log(rect)
      var res = wx.getSystemInfoSync();
      let targetOffSetLeft = offSetLeft - (res.windowWidth / 2) + (rect.width / 2);
      that.setData({
        scrollLeft: targetOffSetLeft,

      });
    }).exec();
  },



  moveClick: function (index) {
    var animation = wx.createAnimation({
      duration: 200,
      delay: 0,
      timingFunction: "ease",
    });

    animation.translate((windowWidth / 3 * index), 0).step({ duration: 200 })
    this.setData({
      moveData: animation.export()
    })
  },


  setListData() {
    var hData = []
    var nData = []
    for (var i = 0; i < this.data.allDrugData.length; i++) {
      if (this.data.allDrugData[i].state == 0) {
        nData.push(this.data.allDrugData[i])
      } else {
        hData.push(this.data.allDrugData[i])
      }
    }
    console.log(hData, nData)
    this.setData({
      haveEvaluatedDrugData: hData,
      waitEvaluateDrugData: nData
    })
  },



  clickListAction(e) {
    console.log(e, this.data.activeTab)
    var data = {}
    if (this.data.activeTab == 0) {
      data = this.data.allDrugData[Number(e.currentTarget.id)]
    } else if (this.data.activeTab == 1) {
      data = this.data.waitEvaluateDrugData[Number(e.currentTarget.id)]
    } else {
      data = this.data.haveEvaluatedDrugData[Number(e.currentTarget.id)]
    }
    wx.navigateTo({
      url: '/pages/evaluate/index?commentId=' + data.id,
    })
  }
})